package cn.xiaowenjie.controllers;

import cn.xiaowenjie.beans.CategoryDto;
import cn.xiaowenjie.response.PageResult;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

import javax.validation.Valid;

import cn.xiaowenjie.beans.Category;
import cn.xiaowenjie.common.beans.ResultBean;
import cn.xiaowenjie.services.CategoryService;
import io.swagger.annotations.Api;

/**
 * Created by Administrator on 2020/4/10.
 * <p>
 * by author wz
 * <p>
 * cn.xiaowenjie.controllers
 */
@Api(description = "分类api")
@RequestMapping("/category")
@RestController
@CrossOrigin
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    @PostMapping("/list")
    public List<Category> getList() {
        return categoryService.getList();
    }

    @PostMapping("/all")
    public ResultBean<PageResult<CategoryDto>> getAll() {
        return new ResultBean<PageResult<CategoryDto>>(categoryService.getAll());
    }

    /**
     * 新增配置
     *
     * FIXME 同时支持json格式和表单格式
     *
     * @param category
     * @return
     */
    @PostMapping("/add")
    public ResultBean<Long> add(@RequestBody @Valid Category category) {
        return new ResultBean<Long>(categoryService.add(category));
    }

    @PostMapping("/delete")
    public ResultBean<Boolean> delete(@RequestParam long id) {
        return new ResultBean<Boolean>(categoryService.delete(id));
    }
    @PostMapping("/update")
    public ResultBean<Long> update(@RequestBody @Valid Category category) {
        return new ResultBean<Long>(categoryService.update(category));
    }

    @PostMapping("/query/{parentId}")
    public ResultBean<List<Category>> queryByParentId(@PathVariable Long parentId) {
        List<Category> categories = Lists.newArrayList();
        categories = categoryService.queryByParentId(parentId);
        return new ResultBean<List<Category>>(categories);
    }
}
